package lt.liepasoft.locr;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import lt.liepasoft.locr.data.OcrImage;
import lt.liepasoft.locr.data.RgbImage;
import lt.liepasoft.locr.data.Symbol;
import lt.liepasoft.locr.data.SymbolImage;
import lt.liepasoft.locr.gui.SymbolDrawPanel;
import lt.liepasoft.locr.realization.EncryptedSymbols;
import lt.liepasoft.locr.realization.FloodFillScanner;
import lt.liepasoft.locr.utils.ImageUtils;

public class ByteSequenceGUI extends JPanel {

    private SymbolImage img;
    private JEditorPane editorPane;
    private final SymbolDrawPanel 
                    symbolDrawPanel,
                    symbolDrawPanel2;        
    
    public static void main(String[] args) {
        new ByteSequenceGUI();
    }
        
    public ByteSequenceGUI() {
        super(new BorderLayout(15, 15));
//        img = createSymbol("C").getImage();
        symbolDrawPanel = new SymbolDrawPanel(200, 200, true);
        symbolDrawPanel2 = new SymbolDrawPanel(200, 200, false);
        //symbolDrawPanel.setSymbolImage(img);
        symbolDrawPanel.setSymbolImage(new SymbolImage(16,16));        
        
        JPanel centerPanel = new JPanel(new GridLayout(1, 2, 30,30));
        centerPanel.add(symbolDrawPanel);
        centerPanel.add(symbolDrawPanel2);
        add(centerPanel, BorderLayout.CENTER);
        JPanel bottom = new JPanel(new GridLayout(2, 1,10,10));
        JButton b = new JButton("Get Nonogram");
        b.setPreferredSize(new Dimension(80,40));
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editorPane.setText(getNanogramString((symbolDrawPanel.getSymbolImage())));
            }
        });
        bottom.add(b);
        bottom.add(createRecognizedTextPanel());
        add(bottom, BorderLayout.SOUTH);
        

                
        
        JFrame f = new JFrame("Byte sequence");
        f.setSize(600,600);
        f.setContentPane(this);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.pack();
        
        updateUI();
        
//        byte[][][] n = new NonogramEncryptor().encrypt(symbolDrawPanel.getSymbolImage());
//
//        new NonogramDecryptor(nonogramDrawPanel).decrypt1(n);
    }
    
    private JScrollPane createRecognizedTextPanel(){
        editorPane = new JEditorPane(){            
        };
        editorPane.setEditable(false);

        JScrollPane editorScrollPane = new JScrollPane(editorPane);
        editorScrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        editorScrollPane.setPreferredSize(new Dimension(200, 75));
        return editorScrollPane;
    }
    
    public Symbol createSymbol(String letter){
        ImageUtils utils = ImageUtils.getInstance();
        Scanner scanner = new FloodFillScanner();
        
        RgbImage rgb = utils.createLetterImage(letter);
        OcrImage ocr = ImageManager.createOcrImage(rgb, true);
        List <Symbol> symbols = scanner.scan(ocr, ocr.bounds);
        if (symbols.size()>0){
            //System.out.println(letter+": "+symbols.get(0).getImage().getBlackPixels());
            return symbols.get(0);
        }else{            
            System.err.println("Error with: "+letter);            
            return null;
        }
    }     
    
    private String getNanogramString(SymbolImage img){
    	System.out.println(img);
        int[] n = EncryptedSymbols.symbolEncrypt(img);
        symbolDrawPanel2.setSymbolImage(
        		EncryptedSymbols.symbolDecrypt(
                    n, 
                    symbolDrawPanel.getSymbolImage().getWidth(), 
                    symbolDrawPanel.getSymbolImage().height));       
        
        StringBuilder b = new StringBuilder();
        b.append("{");
        for (int i=0; i<n.length; i++){
            b.append(n[i]+",");            
        }
        b.append("},\n");
        return b.toString();
    }
}

